home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Magazine / Online / httpproxy / src / queue.c < prev    next >
C/C++ Source or Header  |  1996-08-20  |  5KB  |  263 lines

  1. /*(( "Header" */
  2. /*
  3.  * $Id: queue.c,v 1.3 1996/08/20 17:36:43 mshopf Exp mshopf $
  4.  *
  5.  * (c) 1995-96 Matthias Hopf
  6.  *
  7.  * Queueing for HttpProxy.
  8.  *
  9.  */
  10.  
  11. /*
  12.  * $Log: queue.c,v $
  13.  * Revision 1.3  1996/08/20  17:36:43  mshopf
  14.  * writing url entries on queuing and dequeuing, too.
  15.  * changed url file format (alpha compatible).
  16.  * small bug fix.
  17.  *
  18.  * Revision 1.2  1996/08/11  22:25:15  mshopf
  19.  * reworked debug messages.
  20.  *
  21.  * Revision 1.1  1996/07/30  13:57:03  mshopf
  22.  * Initial revision
  23.  *
  24.  *
  25.  */
  26.  
  27.  
  28. /*)) */
  29. /*(( "Queuefile format" */
  30.  
  31. /*
  32.  * Format of the queue file in EBNF:
  33.  *
  34.  * Queuefile      ::= { QueueEntry }*
  35.  * QueueEntry     ::= Type Url '\n'
  36.  * Type           ::= AddUrl | RemoveUrl
  37.  * AddUrl         ::= '+' | ''    ('' only for compatibility - will be removed next time)
  38.  * RemoveUrl      ::= '-'
  39.  *
  40.  * Partial semi-format description of all low-level symbols:
  41.  *
  42.  * Url            ::= { printable }*
  43.  */
  44.  
  45. /*)) */
  46. /*(("Includes/Constants" */
  47.  
  48. #include <string.h>
  49.  
  50. #include <exec/exec.h>
  51. #include <dos.h>
  52.  
  53. #include <proto/exec.h>
  54. #include <proto/dos.h>
  55.  
  56. #include "queue.h"
  57. #include "logging.h"
  58.  
  59. /*)) */
  60. /*(( "Globals" */
  61.  
  62. /* global queue table */
  63. static queue_t Queue [MAX_QUEUE_NUM];      /* initialized with '\0' */
  64. static int Initialized = FALSE;
  65.  
  66. /*)) */
  67.  
  68. /*(( "QueueWriteEntry ()" */
  69.  
  70. /* open the queue file and write a queue entry */
  71. /* doesn't do that while we are building the entry table (Initialized==FALSE). */
  72.  
  73. static void QueueWriteEntry (char Type, const char *Url)
  74. {
  75.     BPTR File;
  76.  
  77.     if (Initialized)
  78.     {
  79.     if (! (File = Open (QUEUEFILE, MODE_READWRITE)) )
  80.     {
  81.         LogErr (NULL, L_WARN, NULL, -1, "cannot open queuefile");
  82.         return;
  83.     }
  84.     SetIoErr (0);
  85.     Seek  (File, 0, OFFSET_END);
  86.     FPutC (File, (LONG) Type);
  87.     FPuts (File, (char *) Url);
  88.     FPutC (File, '\n');
  89.     if (IoErr ())
  90.         LogErr (NULL, L_ERROR, NULL, -1, "error while writing to queuefile");
  91.     Close (File);
  92.     }
  93. }
  94.  
  95. /*)) */
  96.  
  97. /*(( "QueueInit ()" */
  98.  
  99. /* Initialization. Read in queuing information */
  100.  
  101. int QueueInit (void)
  102. {
  103.     int  i = 0;
  104.     BPTR File;
  105.     char *c;
  106.  
  107.     if (Initialized)
  108.     return 1;
  109.     if (! (File = Open (QUEUEFILE, MODE_OLDFILE)) )
  110.     {
  111.     LogErr (NULL, L_WARN, NULL, -1, "cannot open queuefile");
  112.     Initialized = TRUE;
  113.     return 0;
  114.     }
  115.  
  116.     do
  117.     if (FGets (File, Queue [i] .Url, MAX_QUEUE_LEN))
  118.     {
  119.         if ( (c = strrchr (Queue [i] .Url, '\n')) )
  120.         *c = 0;
  121.         switch (Queue [i] .Url [0]) {
  122.         case '+':
  123.             /* move Url one byte to the left (including '\0') */
  124.         memmove (& Queue [i] .Url [0], & Queue [i] .Url [1], strlen (Queue [i] .Url));
  125.         break;
  126.         case '-':
  127.             /* remove Url from list. The current entry won't be found because it starts with '-' */
  128.         QueueUnqueue (QueueCheck (& Queue [i] .Url [1]));
  129.         Queue [i--] .Url [0] = '\0';         /* to be filled again */
  130.         break;
  131.         default:
  132.             /* After compatibility remove: error message */
  133.         break;
  134.         }
  135.         debug (D_QUEUE, ("init: '%s'\n", Queue [i] .Url));
  136.     }
  137.     else
  138.         break;
  139.     while (++i < MAX_QUEUE_NUM);
  140.  
  141.     i = 1;
  142.     if (IoErr ())
  143.     {
  144.     LogErr (NULL, L_ERROR, NULL, -1, "error while reading queuefile");
  145.     i = 0;
  146.     }
  147.     Close (File);
  148.     Initialized = TRUE;
  149.     return i;
  150. }
  151.  
  152. /*)) */
  153. /*(( "QueueExit ()" */
  154.  
  155. /* Clean up. Save queuing information */
  156.  
  157. void QueueExit (void)
  158. {
  159.     int i;
  160.     BPTR File;
  161.  
  162.     if (! Initialized)
  163.     return;
  164.     Initialized = FALSE;
  165.  
  166.     if (! (File = Open (QUEUEFILE "@new", MODE_NEWFILE)) )
  167.     {
  168.     LogErr (NULL, L_WARN, NULL, -1, "cannot open new queuefile");
  169.     return;
  170.     }
  171.  
  172.     SetIoErr (0);
  173.     for (i = 0; i < MAX_QUEUE_NUM; i++)
  174.     if (Queue [i] .Url [0] != '\0')
  175.     {
  176.         FPutC (File, '+');
  177.         FPuts (File, Queue [i] .Url);
  178.         FPutC (File, '\n');
  179.         debug (D_QUEUE, ("exit: '%s'\n", Queue [i] .Url));
  180.         Queue [i] .Url [0] = '\0';
  181.     }
  182.  
  183.     if (IoErr ())
  184.     LogErr (NULL, L_ERROR, NULL, -1, "error while writing new queuefile");
  185.     Close (File);
  186.  
  187.     DeleteFile (QUEUEFILE);
  188.     Rename (QUEUEFILE "@new", QUEUEFILE);
  189. }
  190.  
  191. /*)) */
  192. /*(( "QueueCheck ()" */
  193.  
  194. /* Check whether a specific Url is already queued. */
  195.  
  196. queue_t *QueueCheck (const char *Url)
  197. {
  198.     int i;
  199.     debug (D_QUEUE, ("check: Url '%s'\n", Url));
  200.  
  201.     for (i = 0; i < MAX_QUEUE_NUM; i++)
  202.     if (strcmp (Url, Queue [i] .Url) == 0)
  203.         return (& Queue [i]);
  204.     return (NULL);
  205. }
  206.  
  207. /*)) */
  208. /*(( "QueueQueue ()" */
  209.  
  210. /* Queue a Url. */
  211.  
  212. int QueueQueue (const char *Url)
  213. {
  214.     int i;
  215.     debug (D_QUEUE, ("queue: Url '%s'\n", Url));
  216.  
  217.     if (strlen (Url) >= MAX_QUEUE_LEN)
  218.     dreturn (D_QUEUE, 0);
  219.  
  220.     if (QueueCheck (Url))
  221.     dreturn (D_QUEUE, 1);
  222.  
  223.     for (i = 0; i < MAX_QUEUE_NUM; i++)
  224.     if (Queue [i] .Url [0] == '\0')
  225.     {
  226.         strcpy (Queue [i] .Url, Url);
  227.         QueueWriteEntry ('+', Url);
  228.         dreturn (D_QUEUE, 1);
  229.     }
  230.     dreturn (D_QUEUE, 0);
  231. }
  232.  
  233. /*)) */
  234. /*(( "QueueUnqueue ()" */
  235.  
  236. /* Remove a queued Url. */
  237.  
  238. void QueueUnqueue (queue_t *q)
  239. {
  240.     if (! q)
  241.     return;
  242.     debug (D_QUEUE, ("unqueue: Url '%s'\n", q->Url));
  243.     QueueWriteEntry ('-', q->Url);
  244.     q->Url [0] = '\0';
  245. }
  246.  
  247. /*)) */
  248. /*(( "QueueNext ()" */
  249.  
  250. /* Enumeration function. Return next queued Url. */
  251.  
  252. queue_t *QueueNext (const queue_t *Last)
  253. {
  254.     const queue_t *q;
  255.     for (q = Last ? Last + 1 : Queue; q < & Queue [MAX_QUEUE_NUM]; q++)
  256.     if (q->Url [0] != '\0')
  257.         return q;
  258.     return NULL;
  259. }
  260.  
  261. /*)) */
  262.  
  263.